package Base.ExceptionTest.ThrowTest;

import Base.ExceptionTest.MyException;

/**
 * 手动抛出异常
 */
public class StudentTest {
    public static void main(String[] args) throws Exception {
        try {
            final Student student = new Student();
            student.regist(-1001);
            System.out.println(student);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}

class Student{
    int id;
    public void regist(int id) throws Exception {
        if (id > 0 ){
            this.id = id;
        }else {
            // 只是判断了改参数不合法
//            System.out.println("该参数不合法!!!");
            // 手动抛出异常
//            throw new Exception("该参数不合法!!!");
            // 自己编写的异常类
            throw new MyException("不能输入负数！");
        }
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}
